package com.xizi.forum.config;

import com.xizi.forum.common.AppResult;
import com.xizi.forum.common.ResultCode;
import com.xizi.forum.exception.ApplicationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
@ResponseBody
@Slf4j
public class ErrorAdvice {
    @ExceptionHandler
    public AppResult exception(Exception e){
        e.printStackTrace();
        return AppResult.fail(e.getMessage());
    }

    @ExceptionHandler
    public AppResult exception(ApplicationException e){
        ResultCode resultCode = e.getResultCode();
        log.error("错误信息： {}",resultCode);

        if (resultCode == null){
            if(e.getMessage().length() > 0){
                return AppResult.fail(e.getMessage());
            }
            return AppResult.fail(ResultCode.FAILED);
        }
        return AppResult.fail(e.getResultCode());
    }
}
